import pyglet
from pyglet.window import key
from config import *


class Bird(pyglet.sprite.Sprite):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.y = 400
        self.x = 400
        self._die = False
        self.v = 0.0

    def update(self, t):
        t = t * 60
        dy = self.v * t + 0.5 * g * t ** 2
        self.v = self.v + g * t
        self.y = self.y + dy
        if self.v < 0:
            self.rotation = (self.v / -16) * 45
        else:
            self.rotation = (self.v / 16) * (-45)

    def on_key_press(self, symbol, modifiers):
        if symbol == key.SPACE and self._die is False:
            self.v = -16 * g

    def die(self):
        self._die = True
